package com.wuseguang.springcloud.controller;


import com.alibaba.fastjson.JSON;
import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.BanCardsService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @描述 班牌开关机和首页接口
 * @创建人 zm
 * @创建时间 2020/2/14
 */
@RestController
@RequestMapping("/brands")
public class BanCardsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BanCardsService banCardService;


    @Autowired
    private ClassCardService classCardService;

    /**
     * @描述 先判断是否有常规和节假日时间，最后在处理两个条件同时存在的情况
     * @创建人 zm
     * @创建时间 2020/1/7
     */
    @RequestMapping("/querySwitchingTime")
    public Result querySwitchingTime(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("deviceid={}", deviceid);
        Map<String, String> map = banCardService.SwitchingTime(deviceid);
        if (map == null || map.size() <= 0) {
            return new Result();
        }
        return new Result(map);
    }

    /**
     * @描述 获取天气预报
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/queryWeather")
    public Result queryWeather(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryWeather-->deviceid={}", deviceid);
        String s = banCardService.queryWeather(deviceid);
        if (!StringUtils.isEmpty(s)) {
            Map<String, Object> parse = (Map<String, Object>) JSON.parse(s);
            return new Result(parse.get("data"));
        }
        return new Result();
    }

    /**
     * @描述 更新时间心跳 ，用于判断 在线/离线
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/updateHeartBeatTime")
    public Result updateHeartBeatTime(@RequestParam(value = "deviceid") String deviceid) {
        return new Result(banCardService.updateHeartBeatTime(deviceid));
    }

    /**
     * @描述 班主任信息
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/queryClassInformation")
    public Result queryClassInformation(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryClassInformation-->deviceid={}", deviceid);
        return new Result(banCardService.queryClassInformation(deviceid));
    }


    /**
     * @描述 查询学校/班级信息
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/querySchoolInformation")
    public Result querySchoolInformation(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("querySchoolInformation-->deviceid={}", deviceid);
        return new Result(banCardService.querySchoolInformation(deviceid));
    }

    /**
     * @描述 查询班级图片
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/queryClassPictures")
    public Result queryClassPictures(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryClassPictures-->deviceid={}", deviceid);
        return new Result(banCardService.queryClassPictures(deviceid));
    }

    /**
     * @描述 查看班级视频
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/queryClassVideos")
    public Result queryClassVideos(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryClassVideos-->deviceid={}", deviceid);
        return new Result(banCardService.queryClassVideos(deviceid));
    }

    /**
     * @描述 查看班级通知
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/queryClassNotices")
    public Result queryClassNotices(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryClassNotices-->deviceid={}", deviceid);
        return new Result(banCardService.queryClassNotices(deviceid));
    }

    /**
     * @描述 修改心跳接口
     * @创建人 zm
     * @创建时间 2020/2/15
     */
    @RequestMapping("/updateHeartbeatType")
    public Result updateHeartbeatType(@RequestParam(value = "deviceid") String deviceid, @RequestParam(value = "type") String type) {
        logger.info("updateHeartbeatType-->deviceid={},type={}", deviceid, type);
        try {
            int i = banCardService.updateHeartbeat(deviceid, type);
            if (i > 0) {
                return new Result();
            }
        } catch (Exception e) {
            logger.info("error={}", e);
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }


    /**
     * @描述 绑定推送
     * @创建人 zm
     * @创建时间 2020/2/16
     */
    @RequestMapping("/updatePush")
    public Result updatePush(@RequestParam(value = "deviceid") String deviceid, @RequestParam(value = "clientid") String clientid) {
        logger.info("updatePush-->deviceid={},clientid={}", deviceid, clientid);
        int i = banCardService.updatePush(deviceid, clientid);
        if (i > 0) {
            classCardService.deviceidAndCards(deviceid);//绑定成功后，进行推送
            return new Result();
        }
        return new Result(CodeUtil.BINDING_FAILED);
    }

    /**
     * @描述 查询课表
     * @创建人 zm
     * @创建时间 2020/2/23
     */
    @RequestMapping("/queryTimetable")
    public Result queryTimetable(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryTimetable-->deviceid={}", deviceid);
        return new Result(banCardService.queryTimetable(deviceid));
    }

    /**
     * @描述 查询班级值日
     * @创建人 zm
     * @创建时间 2020/3/15
     */
    @RequestMapping("/queryTodayDuty")
    public Result queryTodayDuty(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryTodayDuty-->deviceid={}", deviceid);
        return new Result(banCardService.queryTodayDuty(deviceid));
    }

    /**
     * @描述 查询所有值日信息
     * @创建人 zm
     * @创建时间 2020/4/22
     */
    @RequestMapping("/queryDutyAll")
    public Result queryDutyAll(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryDutyAll-->deviceid={}", deviceid);
        return banCardService.queryDutyAll(deviceid);
    }


    /**
     * @描述 查所有应用模块
     * @创建人 zm
     * @创建时间 2020/3/16
     */
    @RequestMapping("/queryApplicationAll")
    public Result queryApplicationAll(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("queryApplicationAll-->deviceid={}", deviceid);
        return new Result(banCardService.queryApplicationAll(deviceid));
    }
}
